
<%@page import="org.lxy.service.UserInfoService"%>
<%@page import="org.lxy.service.RecycleGoodsService"%>
<%@page import="org.lxy.domain.RecycleGoodsEntity"%>
<%@page import="org.lxy.service.GoodsTypeService"%>
<%@page import="org.lxy.domain.GoodsTypeEntity"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%@page import="org.lxy.domain.UserInfoEntity"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%@ page import="org.lxy.utils.CoreUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	response.setContentType("text/html; charset=UTF-8");
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	if (isMultipart) {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		try {
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> it = items.iterator();
			String dizhi = "";
			int duihuanjifen = 0;
			int huishoudianid = 0;
			int huisoufangshi = 0;
			int id = 0;
			String jieshao = "";
			int leixingid = 0;
			String mingcheng = "";
			String shangmenhuishoushijian = "";
			int xuyaojifen = 0;
			String zhuangtai = "";
			String huishoufangshi = "";
			String fname = "";
			String xiangqing = "";
			while (it.hasNext()) {
				FileItem item = it.next();
				String itemname = item.getFieldName();

				if (item.isFormField()) {
					if (itemname.equals("dizhi")) {
						dizhi = item.getString("utf-8");
					} else if (itemname.equals("huishoudianid")) {
						huishoudianid = Integer.parseInt(item.getString("utf-8"));

					} else if (itemname.equals("huishoufangshi")) {
						huishoufangshi = item.getString("utf-8");
					} else if (itemname.equals("jieshao")) {
						jieshao = item.getString("utf-8");
					} else if (itemname.equals("leixingid")) {
						leixingid = Integer.parseInt(item.getString("utf-8"));
					} else if (itemname.equals("mingcheng")) {
						mingcheng = item.getString("utf-8");
					} else if (itemname.equals("shangmenhuishouishijian")) {
						shangmenhuishoushijian = item.getString("utf-8");
					} else if (itemname.equals("id")) {
						id = Integer.parseInt(item.getString("utf-8"));
					} else {
						System.out.println("其他字段");
					}
				} else {
					fname = item.getName();
					System.out.println("fname=" + fname);
					String uploadpath = request.getSession().getServletContext().getRealPath("upload");
					// String path = "E:\\upload";
					System.out.println("path=" + uploadpath);
					if (!fname.isEmpty()) {
						File file = new File(uploadpath, fname);
						item.write(file);
					}
					//request.setAttribute("news", fname + "上传成功,谢谢分享");
					// request.getRequestDispatcher("print.jsp").forward(request,response);
					// System.out.println(filename+"上传成功!!!");
					// return;
				}

			}
			RecycleGoodsEntity entity = new RecycleGoodsEntity();
			RecycleGoodsService service = CoreUtils.recycleGoodsService;
			UserInfoEntity account = (UserInfoEntity) request.getSession().getAttribute("Account");

			if (account == null) {
				out.write("<script>window.top.location.href='/login';</script>");
				return;
			}
			if (id > 0) {
				entity = service.getById(id);
			} else {
				entity.setId(0);
				entity.setImg("");
				entity.setStatus("0");
			}
			entity.setAddress(dizhi);
			entity.setChangePoints(duihuanjifen);
			entity.setRecyclePointId(huishoudianid);
			entity.setRecycleMethod(huishoufangshi);
			entity.setId(id);
			entity.setInfo(jieshao);
			entity.setTypeId(leixingid);
			entity.setName(mingcheng);
			entity.setOnSiteRecycleTime(shangmenhuishoushijian);
			entity.setUserId(account.getId());
			entity.setRecyclePoints(xuyaojifen);
			entity.setStatus("待审核");
			if (!fname.isEmpty()) {
				entity.setImg(fname);
			}

			boolean tag = false;
			if (entity.getId() > 0) {
				tag = service.updateById(entity);
			} else {
				tag = service.save(entity);
				account.setPoints(account.getPoints()+entity.getRecyclePoints());
				CoreUtils.userInfoService.updateById(account);
				request.getSession().setAttribute("Account", account);
			}
			System.out.println("回收商品:"+ JSONObject.toJSONString(entity));
			if (tag) {
				if(huishoufangshi.equals("回收点回收")){
				out.write("<script>alert('操作成功!');window.location.href='/huishoudianhuishou"
						+ "';</script>");
				}else{
					out.write("<script>alert('操作成功!');window.location.href='/shangmenhuishou"
							+ "';</script>");
				}

			} else {
				out.write("<script>alert('操作失败!');</script>");

			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
%>